package com.yixun.service.impl;

import com.alibaba.fastjson.JSON;
import com.yixun.entity.YxUser;
import com.yixun.entity.YxUserRole;
import com.yixun.mapper.UserRoleMapper;
import com.yixun.service.LogSetService;
import com.yixun.service.UserRoleService;
import com.yixun.util.FormatUtil;
import com.yixun.util.LogUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service
public class UserRoleServiceImpl implements UserRoleService {

    private final UserRoleMapper userRoleMapper;

    private final LogSetService logSetService;

    public UserRoleServiceImpl(UserRoleMapper userRoleMapper, LogSetService logSetService) {
        this.userRoleMapper = userRoleMapper;
        this.logSetService = logSetService;
    }


    @Override
    @Transactional
    public int insertUserRole(YxUser sessionUser, YxUserRole userRole) {
        int row;
        row = userRoleMapper.delUserRoleByUserId(userRole.getUser_id());
        String role_ids = userRole.getRole_id();
        if (null != role_ids && !role_ids.equals("")) {
            YxUserRole paramUR = new YxUserRole();
            paramUR.setUser_id(userRole.getUser_id());
            String[] roleIds = role_ids.split(",");
            for (String role_id: roleIds) {
                paramUR.setUser_role_id(new FormatUtil().getUUID());
                paramUR.setRole_id(role_id);
                userRoleMapper.insertUserRole(paramUR);
                row++;
            }
        } else {
            row++;
        }
        if (row > 0) {
            logSetService.insertSettingLogs(LogUtil.ADD, "用户关联角色", JSON.toJSONString(userRole), sessionUser);
            return row;
        }
        return row;
    }

    @Override
    public String getUserRoleID(String user_id) {
        return userRoleMapper.getUserRoleID(user_id);
    }

    @Override
    public String getUserIdsByRoleId(String r_id) {
        return userRoleMapper.getUserIdsByRoleId(r_id);
    }

    @Override
    @Transactional
    public int insertUserRoleByRole(YxUser sessionUser, Map map) {
        int row = 0;
        String role_id = (String) map.get("role_id");
        String delete_user_ids = (String) map.get("delete_user_ids");
        String user_ids = (String) map.get("user_id");
        YxUserRole userRole = new YxUserRole();
        userRole.setRole_id(role_id);
        if (null != delete_user_ids && !delete_user_ids.equals("")) {
            String[] delUserIds = delete_user_ids.split(",");
            for (String delUserId: delUserIds) {
                userRole.setUser_id(delUserId);
                userRoleMapper.delUserRoleByUserIdAndRoleId(userRole);
                row++;
            }
        }
        if (null != user_ids && !user_ids.equals("")) {
            String[] userIds = user_ids.split(",");
            for (String userId: userIds) {
                userRole.setUser_role_id(new FormatUtil().getUUID());
                userRole.setUser_id(userId);
                userRoleMapper.insertUserRole(userRole);
                row++;
            }
        }
        if (row > 0) {
            logSetService.insertSettingLogs(LogUtil.UPDATE, "角色关联用户", JSON.toJSONString(map), sessionUser);
            return row;
        }
        return row;
    }
}
